package com.zmc.coupon.controller;

import java.util.List;
import java.util.Objects;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.coupon.infrastructure.bean.query.LadderQuery;
import com.zmc.coupon.infrastructure.bean.vo.SkuLadderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zmc.coupon.infrastructure.entity.SkuLadderEntity;
import com.zmc.coupon.infrastructure.service.SkuLadderService;
import com.zmc.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 商品阶梯价格
 *
 * @author zmc
 * @email 
 * @date 2024-12-04 15:36:59
 */
@RestController
@RequestMapping("/ladder")
public class SkuLadderController {
    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody LadderQuery query){
        return R.page(skuLadderService.queryPage(query));
    }

    /**
     * 导出列表
     */
    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, LadderQuery query) {
        List<SkuLadderEntity> list = skuLadderService.queryList(query);
        ExcelUtil<SkuLadderEntity> util = new ExcelUtil<>(SkuLadderEntity.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody SkuLadderVo vo){
        return R.ok(skuLadderService.getById(vo.getId()));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R save(@RequestBody SkuLadderVo vo){
        SkuLadderEntity entity = skuLadderService.getBySkuId(vo.getSkuId());
        if (entity != null) {
            return R.error("当前商品已参与其它打折活动");
        }
        vo.setMerchantId(authTokenService.getUserInfo().getMerchantId());
        SkuLadderEntity skuLadder = new SkuLadderVo();
        BeanUtils.copyProperties(vo, skuLadder);
        skuLadderService.save(skuLadder);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R update(@RequestBody SkuLadderEntity skuLadder){
        SkuLadderEntity ladder = skuLadderService.getById(skuLadder.getId());
        if (!Objects.equals(ladder.getSkuId(), skuLadder.getSkuId())) {
            SkuLadderEntity entity = skuLadderService.getBySkuId(skuLadder.getSkuId());
            if (entity != null) {
                R.error("当前商品已参与其它打折活动");
            }
        }
		skuLadderService.updateById(skuLadder);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R delete(@RequestBody SkuLadderVo vo){
		skuLadderService.removeById(vo.getId());
        return R.ok();
    }

}
